package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.CollectionActivityAdapter;
import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.Dialog;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.CollectionModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.DetailsCreditActivity;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/30.
 * 收藏
 */

public class CollectionActivity extends Activity implements View.OnClickListener, Callback.CommonCallback<String> {
    @Bind(R.id.retur)
    LinearLayout retur;

    @Bind(R.id.listview)
    ListView listview;

    @Bind(R.id.available_layout)//无消息提示
            LinearLayout available_layout;
    List<CollectionModel> getcollection=new ArrayList<>();
    CollectionActivityAdapter adapter;
    String Judge_interface = "0";//判断哪个接口   0、收藏展示 1、收藏取消
    Context context;
    private MyProgressDialog mDialog;
    AlertDialog alertDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection);
        ButterKnife.bind(this);
        context = this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);

        adapter = new CollectionActivityAdapter(context);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(clickListener);


    }

    AdapterView.OnItemClickListener clickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            startActivity(new Intent(context, DetailsCreditActivity.class));

        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        processcreditScore();
    }

    //我的收藏
    private void processcreditScore() {
        if (Sundry.isOpenNetwork(context)) {
            mDialog = new MyProgressDialog(context);
            Judge_interface = "0";
            HttpClient.collection(this);
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }

    //取消收藏
    public void processcancel(final String id) {
        alertDialog= Dialog.phonedialog(context, "确认取消收藏吗", "取消收藏", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Sundry.isOpenNetwork(context)) {
                    mDialog = new MyProgressDialog(context);
                    Judge_interface = "1";
                    HttpClient.cancel(id, CollectionActivity.this);
                } else {
                    ToastUtils.Toastshow(context, getResources().getString(R.string.network));
                }
                alertDialog.dismiss();
            }
        },"确定","取消");

    }

    @Override
    public void onSuccess(String result) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        System.out.println("stringmd5result" + result);
        List<RegistrationModel> getnote = JsonTools.getunderlying(result);
        if (getnote != null || getnote.size() > 0) {
            String status = getnote.get(0).getStatus();
            String getMsg = getnote.get(0).getMsg();
            if (status.equals("200")) {
                if (Judge_interface.equals("0")) {

                    String content = getnote.get(0).getContent();
                    if (!TextUtils.isEmpty(content)){
                        available_layout.setVisibility(View.GONE);
                        listview.setVisibility(View.VISIBLE);
                        getcollection = JsonTools.getcollection(content);
                        if (getcollection != null || getcollection.size() > 0) {
                            adapter.addItem(getcollection);
                        }
                    }else {
                        available_layout.setVisibility(View.VISIBLE);
                        listview.setVisibility(View.GONE);
                        ToastUtils.show(context, getMsg);
                    }
                } else {
                    ToastUtils.show(context, getMsg);
                    processcreditScore();
                }
            } else if (status.equals("105")) {
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                ToastUtils.show(context, getMsg);
            } else {
                ToastUtils.show(context, getMsg);
            }
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        if(Judge_interface.equals("0")){
            available_layout.setVisibility(View.VISIBLE);
            listview.setVisibility(View.GONE);
        }
        System.out.println("stringmd5result" + ex.getMessage().toString());
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        if(Judge_interface.equals("0")){
            available_layout.setVisibility(View.VISIBLE);
            listview.setVisibility(View.GONE);
        }
        System.out.println("stringmd5result" + cex.getMessage().toString());
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }
}

